const service = require('../request').service;
const model = require("../../utils/modelUtil");

/**
 * 商店
 */
class Shopping {
    constructor(user) {
        this.user = user;
    }

    /**
     * 获取商店列表
     * @returns {Promise<unknown>}
     */
    getMarketList() {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/GetMarketList',
                data: model.userModel('GetMarketList', {}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }

    /**
     * 购买商品物品
     * @param id 物品ID
     * @param number 购买数量
     * @returns {Promise<unknown>}
     */
    buyMarketItem(id, number) {
        return new Promise((resolve, reject) => {
            service({
                method: 'POST',
                url: this.user.game.reqUrl + '/BuyMarketItem',
                data: model.userModel('BuyMarketItem', {ID: id, itemNum: number}, this.user.game.isWeb, this.user.game.data),
            }).then(res => {
                const resData = res.data;
                resolve(resData.code===200?resData.MsgData:{});
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = Shopping